package com.tfj.mvp.tfj.msg;

import android.os.Bundle;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.hyphenate.tfj.R;
import com.tfj.mvp.base.BaseFragment;
import com.tfj.utils.ViewFindUtils;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by HuangQiang on 2017/12/21.
 *
 * @author HuangQiang
 * @github https://github.com/HuangQiang
 */

public class VMsgParentFragment extends BaseFragment<PMsgImpl> implements CMsg.IVMsg, OnTabSelectListener {
    Unbinder unbinder;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "系统消息", "我的客户"
    };
    private MyPagerAdapter mAdapter;
    public static VMsgParentFragment newInstance() {
        VMsgParentFragment fragment = new VMsgParentFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    protected void initView() {

    }


    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        for (String title : mTitles) {
            mFragments.add(new VMsgFragment(title));
        }
        View decorView = getActivity().getWindow().getDecorView();
        ViewPager vp = ViewFindUtils.find(decorView, R.id.vp);
        mAdapter = new MyPagerAdapter(getChildFragmentManager());
        vp.setAdapter(mAdapter);
        SlidingTabLayout tabLayout = ViewFindUtils.find(decorView, R.id.tl);
        tabLayout.setViewPager(vp);
        vp.setCurrentItem(0);
        TextView textView_0 = tabLayout.getTitleView(0);
        TextView textView_1 = tabLayout.getTitleView(1);
        TextPaint tp_0 = textView_0.getPaint();
        TextPaint tp_1 = textView_1.getPaint();
        textView_0.setTextSize(22);
        textView_1.setTextSize(15);
        tp_0.setFakeBoldText(true);
        tp_1.setFakeBoldText(false);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if(position==0){
                    textView_0.setTextSize(22);
                    textView_1.setTextSize(15);
                    tp_0.setFakeBoldText(true);
                    tp_1.setFakeBoldText(false);
                }else{
                    textView_0.setTextSize(15);
                    textView_1.setTextSize(22);
                    tp_0.setFakeBoldText(false);
                    tp_1.setFakeBoldText(true);
                }
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_msg_par;
    }

    @Override
    public void createPresenter() {
        mPresenter = new PMsgImpl(mContext, this);
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onTabSelect(int position) {
        showToast("position-->"+position);
    }

    @Override
    public void onTabReselect(int position) {

    }
    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
